import dayjs from 'dayjs';
import { sm2 } from 'sm-crypto'

//DATE:
export function dateTimeRangeToStr(startDate: Date | string, endDate: Date | string, formateStr: string = 'YYYY-MM-DD HH:mm:ss') {
	return [dayjs(startDate).format(formateStr), dayjs(endDate).add(1, 'day').second(-1).format(formateStr)];
}

export function dateStr(date: Date | string, formateStr: string = 'YYYY-MM-DD') {
	return dayjs(date).format(formateStr);
}

//STRING:
export function isNullOrEmpty(str: string) {
	return str == null || str == '';
}
export function isNotNullAndEmpty(str: string) {
	return str != null && str != '';
}

export function smEncode(str: string) {
	return "04" + sm2.doEncrypt(str, "04628474A2F6D6110C77302C965B11FB722402122C6320E35E0CDFCBFF87B6FE08DC6A0B2A77122BE440DE08FD64B3E5B516ECA9B055A97A65FA70CD4F2FDB1662", 1);
}

export function downloadFile(data:any, sheetName:string, type:string = "application/vnd.ms-excel;charset=utf-8"){
	let blob = new Blob([data], {
        type: type
	});
    let downloadElement = document.createElement("a");
    let href = window.URL.createObjectURL(blob);
    downloadElement.href = href;
    downloadElement.download = sheetName;
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement); // 下载完成移除元素
    window.URL.revokeObjectURL(href); // 释放掉blob对象
}



